package edu.zut.software.identity.Service.impl;

import edu.zut.software.identity.Repository.TeaInfoRepository;
import edu.zut.software.identity.Repository.TeacherRepository;
import edu.zut.software.identity.Service.TeacherService;
import edu.zut.software.identity.domain.StuInfo;
import edu.zut.software.identity.domain.StuMajor;
import edu.zut.software.identity.domain.Teacher;
import edu.zut.software.identity.domain.TeacherInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * @author Weining
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    private TeacherRepository teacherRepository;
    private TeaInfoRepository teaInfoRepository;
    @Autowired
    public void setTeaInfoRepository(TeaInfoRepository teaInfoRepository) {
        this.teaInfoRepository = teaInfoRepository;
    }

    @Autowired
    public void setTeacherRepository(TeacherRepository teacherRepository) {
        this.teacherRepository = teacherRepository;
    }

    @Override
    public Teacher findById(String id) {
        return teacherRepository.findById(id).orElseThrow(RuntimeException::new);
    }

    @Override
    public List<Teacher> findAll() {
        return teacherRepository.findAll();
    }

    @Override
    public void deleteById(String id) {
        teacherRepository.deleteById(id);
    }

    @Override
    public Teacher save(Teacher teacher) {
         return teacherRepository.save(teacher);
    }

    @Override
    public boolean isExists(String id) {
        return teacherRepository.existsById(id);
    }

    @Override
    public Page<Teacher> findAll(Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize);
        return teacherRepository.findAll(pageable);
    }

    @Override
    public Page<TeacherInfo> findAll(Integer page, Integer pageSize, TeacherInfo teacherInfo) {
        Pageable pageable = PageRequest.of(page, pageSize);
        Page<TeacherInfo> tPage = teaInfoRepository.findAll(new Specification<TeacherInfo>(){

            @Override
            public Predicate toPredicate(Root<TeacherInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(null != teacherInfo.getCollegeName() && !Objects.equals(teacherInfo.getCollegeName(), "")){
                    list.add(criteriaBuilder.like(root.get("collegeName").as(String.class), "%" + teacherInfo.getCollegeName() + "%"));
                }
                if(null != teacherInfo.getMajorName() && !Objects.equals(teacherInfo.getMajorName(), "")){
                    list.add(criteriaBuilder.like(root.get("majorName").as(String.class), "%" + teacherInfo.getMajorName() + "%"));
                }

                if(null != teacherInfo.getTname() && !Objects.equals(teacherInfo.getTname(), "")){
                    list.add(criteriaBuilder.like(root.get("tname").as(String.class), "%" + teacherInfo.getTname() + "%"));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        }, pageable);

        return tPage;
    }

    @Override
    public List<Map<Date, Integer>> getCount() {
        return teacherRepository.getCount();
    }
}
